//
//  HomeTableViewController.swift
//  SwiftWeibo
//
//  Created by Lucky on 16/4/23.
//  Copyright © 2016年 Lucky. All rights reserved.
//

import UIKit
import AFNetworking

let HomeResuseIdentifier = "HomeResuseIdentifier"

class HomeTableViewController: BaseTableViewController {
    
    var status: [Status]? {
        didSet {
            tableView.reloadData()
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        if !userLogin {
            visitorView?.setupVisitorInfo(true, imageName: "visitordiscover_feed_image_house", message: "关注一些人，回这里看看有什么惊喜")
            return
        }
        
        //初始化导航条
        setupNav()
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(HomeTableViewController.change), name: PopoverAnimatorWillShow, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(HomeTableViewController.change), name: PopoverAnimatorWillDismiss, object: nil)
        
        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: HomeResuseIdentifier)
        
        loadData()
    }
    
    func change() -> () {
        let titbleBtn = navigationItem.titleView as! TitleButton
        titbleBtn.selected = !titbleBtn.selected
    }
    
    private func setupNav() {
        //左右按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem.createBarButtonItem("navigationbar_friendattention", target: self, action: #selector(HomeTableViewController.leftItemClick))
        navigationItem.rightBarButtonItem = UIBarButtonItem.createBarButtonItem("navigationbar_pop", target: self, action: #selector(HomeTableViewController.rightItemClick))
        //中间按钮
        let titleBtn = TitleButton()
        titleBtn.setTitle("Joker ", forState: UIControlState.Normal)
        titleBtn.addTarget(self, action: #selector(HomeTableViewController.titleBtnClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        navigationItem.titleView = titleBtn
    }
    
    func leftItemClick() -> () {
        
    }
    
    func rightItemClick() -> () {
        let sb = UIStoryboard(name: "QRCodeViewController", bundle: nil)
        let vc = sb.instantiateInitialViewController()
        
        presentViewController(vc!, animated: true, completion: nil)
    }
    
    func titleBtnClick(sender: TitleButton) -> () {
        let sb = UIStoryboard(name: "PopoverViewController", bundle: nil)
        let vc = sb.instantiateInitialViewController()
        
        vc?.transitioningDelegate = popoverAnimator
        vc?.modalPresentationStyle = UIModalPresentationStyle.Custom
        
        presentViewController(vc!, animated: true, completion: nil)
    }
    
    // MARK: - 获取微博数据
    private func loadData() {
        Status.loadStatuses({ (models) in
            self.status = models
            }) { (error) in
                
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    private lazy var popoverAnimator: PopoverAnimator = {
        let pa = PopoverAnimator()
        pa.presentFrame = CGRect(x: UIScreen.mainScreen().bounds.size.width * 0.5 - 100, y: 56, width: 200, height: 600)
        return pa
    }()

    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
}

extension HomeTableViewController {
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return status?.count ?? 0
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(HomeResuseIdentifier, forIndexPath: indexPath)
        
        cell.textLabel?.text = status![indexPath.row].text
        
        return cell
    }
}
